package com.eugene.shiftscalendar;

import android.appwidget.AppWidgetProviderInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Point;
import android.util.Log;

/**
 * Created by Eugene on 24.02.14.
 */
public class WidgetSizes {
    public int widthPx = 0;
    public int heightPx = 0;
    public int widthCells = 0;
    public int heightCells = 0;

    public enum WidgetType{
        Title,
        Prev,
        Next,
        Settings,
        Weeks,
        LeftSide,
        RightSide,
        Footer,
        Day,
        Upper
    };

    public int dpFromPx(int px)
    {
        return (int)(px / Resources.getSystem().getDisplayMetrics().density);
    }


    public void recalculate(int cellsX, int cellsY)
    {
        widthCells = (cellsX  + 30) / 70;
        heightCells = (cellsY + 30) / 70;

        countPixels();
    }

    private void countPixels()
    {
        Point point = new Point(0,0);
        float density = Resources.getSystem().getDisplayMetrics().density;
        switch (Resources.getSystem().getConfiguration().orientation) {
            case Configuration.ORIENTATION_PORTRAIT:
                point.x = (int)( 80 * widthCells * density + .5f);
                point.y = (int)(100 * heightCells * density + .5f);
                break;
            case Configuration.ORIENTATION_LANDSCAPE:
                point.x = (int)(106 * widthCells * density + .5f);
                point.y = (int)( 74 * heightCells * density +.5f);
                break;
            //case Configuration.ORIENTATION_SQUARE:
            //case Configuration.ORIENTATION_UNDEFINED:
            default:
        }

        widthPx = point.x;
        heightPx = point.y;
    }

    public Point getSize(WidgetType widget, int theme)
    {
        // Какие вообще есть виджеты:
        //       Виджет           Ширина      Высота
        //    1. Шапка             100          10
        //    2. Дни недели        100          7
        //    4. Граница левая      0           1
        //    5. Граница правая     0           1
        //    6. Дата             100/7        (100-19)/6
        //    7. Футер             100          0

        double perX = 100.0;
        double perY = 100.0*2/24;

        //widget = WidgetType.LeftSide;

        switch(theme){
            case 1: // Glass
                switch(widget){
                    case Title: perX=100.0*14/21; perY=100.0*1/8; break;
                    case Prev: perX=100.0*2/21; perY=100.0*1/8; break;
                    case Next: perX=100.0*2/21; perY=100.0*1/8; break;
                    case Settings: perX=100.0*3/21; perY=100.0*1/8; break;
                    case Weeks: perX=100.0; perY=100.0*3/56; break;
                    case LeftSide: perX=0; perY=100.0*11/14; break;
                    case RightSide: perX=0; perY=100.0*11/14; break;
                    case Footer:  perX=100.0; perY=100.0*1/28; break;
                    case Day: perX=100.0*1/7; perY=100.0*11/14/6; break;
                }
                break;
            case 3: // Transparent
            case 4: // Transparent Black
                switch(widget){
                    case Title: perX=100.0*14/21; perY=100.0*50/500; break;
                    case Prev: perX=100.0*2/21; perY=100.0*50/500; break;
                    case Next: perX=100.0*2/21; perY=100.0*50/500; break;
                    case Settings: perX=100.0*3/21; perY=100.0*50/500; break;
                    case Weeks: perX=100.0; perY=100.0*37/500; break;
                    case LeftSide: perX=0; perY=100.0*410/500; break;
                    case RightSide: perX=0; perY=100.0*410/500; break;
                    case Footer:  perX=100.0; perY=0; break;
                    case Day: perX=100.0*1/7; perY=100.0*323/500/6; break;
                }
                break;
            case 5: // Green
                switch(widget){
                    case Title: perX=100.0*14/21; perY=100.0*30/400; break;
                    case Prev: perX=100.0*2/21; perY=100.0*30/400; break;
                    case Next: perX=100.0*2/21; perY=100.0*30/400; break;
                    case Settings: perX=100.0*3/21; perY=100.0*30/400; break;
                    case Weeks: perX=100.0; perY=100.0*15/400; break;
                    case LeftSide: perX=0; perY=100.0*410/500; break;
                    case RightSide: perX=0; perY=100.0*410/500; break;
                    case Footer:  perX=100.0; perY=100.0*20/400; break;
                    case Day: perX=100.0*1/7; perY=100.0*255/400/6; break;
                }
                break;
            case 6: // Notepad
                switch(widget){
                    case Title: perX=100.0*14/21; perY=100.0*50/450; break;
                    case Prev: perX=100.0*2/21; perY=100.0*50/450; break;
                    case Next: perX=100.0*2/21; perY=100.0*50/450; break;
                    case Settings: perX=100.0*3/21; perY=100.0*50/450; break;
                    case Weeks: perX=100.0; perY=100.0*35/450; break;
                    case LeftSide: perX=100.0*12/385; perY=100.0*285/450; break;
                    case RightSide: perX=100.0*12/385; perY=100.0*280/450; break;
                    case Footer:  perX=100.0; perY=100.0*15/450; break;
                    case Day: perX=100.0*361/385/7; perY=100.0*350/450/6; break;
                }
                break;
            case 7: // Paper
                switch(widget){
                    case Upper: perX=100.0; perY=100.0*1/10; break;
                    case Title: perX=100.0*14/21; perY=100.0*1/10; break;
                    case Prev: perX=100.0*2/21; perY=100.0*1/10; break;
                    case Next: perX=100.0*2/21; perY=100.0*1/10; break;
                    case Settings: perX=100.0*3/21; perY=100.0*1/10; break;
                    case Weeks: perX=100.0; perY=100.0*1/10; break;
                    case LeftSide: perX=100.0*1/32; perY=100.0*3/4; break;
                    case RightSide: perX=100.0*1/32; perY=100.0*3/4; break;
                    case Footer:  perX=100.0; perY=100.0*1/20; break;
                    case Day: perX=100.0*30/32/7; perY=100.0*3/4/6; break;
                }
                break;
            default: //Chinese
                switch(widget){
                    case Title: perX=100.0*14/21; perY=100.0*1/10; break;
                    case Prev: perX=100.0*2/21; perY=100.0*1/10; break;
                    case Next: perX=100.0*2/21; perY=100.0*1/10; break;
                    case Settings: perX=100.0*3/21; perY=100.0*1/10; break;
                    case Weeks: perX=100.0; perY=100.0*1/10; break;
                    case LeftSide: perX=100.0*3/100; perY=100.0*39/50; break;
                    case RightSide: perX=100.0*3/100; perY=100.0*39/50; break;
                    case Footer:  perX=100.0; perY=100.0*1/50; break;
                    case Day: perX=100.0*47/50/7; perY=100.0*39/50/6; break;
                }
                break;
        }

       // Log.d("WidgetSizes","W="+(int)(perX*widthPx/100)+" H="+(int)(perY*heightPx/100)+" Type="+widget);

        return new Point((int)(perX*widthPx/100), (int)(perY*heightPx/100));
    }

}
